<template>
  <div class="tabbar">
    <template v-for="(item, index) in tabbarData" :key="index">
      <div
        class="tab-bar-item"
        :class="{ active: currentIndex === index }"
        @click="itemClick(index, item)"
      >
        <img
          v-if="currentIndex !== index"
          :src="getAssetURL(item.image)"
          alt=""
        />
        <img v-else :src="getAssetURL(item.imageActive)" alt="" />
        <span class="text">{{ item.text }}</span>
      </div>
    </template>
  </div>
</template>

<script setup>
import { ref } from "vue"
import { useRouter } from "vue-router"

import tabbarData from "@/assets/data/tabbar.js"
import { getAssetURL } from "@/utils/loadAssetURL.js"

const router = useRouter();

const currentIndex = ref(0);

function itemClick(index, item) {
  currentIndex.value = index;
  router.push(item.path);
}
</script>

<style lang="less" scoped>
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;

  border-top: 1px solid #eee;

  .tab-bar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    &.active {
      color: var(--primary-color);
    }

    img {
      width: 32px;
    }

    .text {
      font-size: 12px;
      margin-top: 2px;
    }
  }
}
</style>